import Vue from 'vue'
import VueRouter from 'vue-router'
import $api from '@/axios/api.js'; // 导入api
import $utils from '@/utils/utils.js' //全局工具类utils
import store from '../store'

Vue.use(VueRouter)

const routes = [
    {
        path: "/login",
        name: "login",
        meta: { index: 1, title: '登录', requiresAuth: false },
        component: () => import("../views/login.vue")
    },
    {
        path: "/login-pwd",
        name: "login-pwd",
        meta: { index: 1, title: '登录', requiresAuth: false },
        component: () => import("../views/login-pwd.vue")
    },
    {
        path: "/login-code",
        name: "login-code",
        meta: { index: 1, title: '登录', requiresAuth: false },
        component: () => import("../views/login-code.vue")
    },
    {
        path: "/register",
        name: "register",
        meta: { index: 1, title: '注册', requiresAuth: false },
        component: () => import("../views/register.vue")
    },
    {
        path: "/forgot",
        name: "forgot",
        meta: { index: 1, title: '找回密码', requiresAuth: false },
        component: () => import("../views/forgot.vue")
    },
    {
        path: '/bind',
        name: '设备绑定',
        meta: { index: 1, title: '设备绑定', requiresAuth: true },
        component: () => import('../views/bind.vue')
    },
    {
        path: '/loading',
        name: '权益',
        meta: { index: 1, title: '权益', requiresAuth: true },
        component: () => import('../views/loading.vue')
    },
    {
        path: '/error',
        name: '错误',
        meta: { index: 1, title: '网站已关闭', requiresAuth: false },
        component: () => import('../views/error.vue')
    },
    {
        path: "/",
        component: () => import("../views/home.vue"),
        redirect: "index",
        children: [
            {
                path: "index",
                name: "首页",
                meta: { index: 1, title: '首页', showTab: true, requiresAuth: true },
                component: () => import("@/views/home/index/index.vue")
            },
            {
                path: "share",
                name: "分享",
                meta: { index: 1, title: '分享', showTab: true, requiresAuth: true },
                component: () => import("@/views/home/share/share.vue")
            },
            {
                path: "mine",
                name: "我的",
                meta: { index: 1, title: '我的', showTab: true, requiresAuth: true },
                component: () => import("@/views/home/mine/mine.vue"),
            },
            {
                path: "trading",
                name: "交易",
                meta: { index: 1, title: '交易数据', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/index/trading.vue")
            },
            {
                path: "placard",
                name: "海报",
                meta: { index: 1, title: '分享海报', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/share/placard.vue")
            },
            {
                path: "team",
                name: "我的团队",
                meta: { index: 1, title: '我的团队', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/index/team.vue")
            },
            {
                path: "order",
                name: "订单数据",
                meta: { index: 1, title: '订单数据', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/index/order.vue")
            },
            {
                path: "tradingDetail",
                name: "交易明细",
                meta: { index: 1, title: '交易明细', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/index/tradingDetail.vue")
            },
            {
                path: "setting",
                name: "设置中心",
                meta: { index: 1, title: '设置中心', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/setting")
            },
            {
                path: "myServicer",
                name: "我的客服",
                meta: { index: 1, title: '我的客服', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/myServicer")
            },
            {
                path: "aboutus",
                name: "关于我们",
                meta: { index: 1, title: '关于我们', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/aboutus")
            },
            {
                path: "modPassword",
                name: "修改密码",
                meta: { index: 1, title: '修改密码', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/modPassword")
            },
            {
                path: "helpRegister",
                name: "协助开通",
                meta: { index: 1, title: '协助开通', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/share/helpRegister")
            },
            {
                path: "registerRes",
                name: "注册通知",
                meta: { index: 1, title: '注册通知', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/registerRes")
            },
            {
                path: "userinfo",
                name: "个人信息",
                meta: { index: 1, title: '个人信息', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/userinfo")
            },
            {
                path: "newbind",
                name: "设备添加",
                meta: { index: 1, title: '设备绑定', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/newbind.vue")
            },
            {
                path: "realname",
                name: "实名认证",
                meta: { index: 1, title: '实名认证', showTab: false, requiresAuth: false },
                component: () => import("@/views/home/mine/realname.vue")
            },
            {
                path: "certifityRes",
                name: "认证结果",
                meta: { index: 1, title: '认证成功', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/certfityRes")
            },
            {
                path: "buyvip",
                name: "权益领取",
                meta: { index: 1, title: '权益领取', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/buyvip")
            },
            {
                path: "payRes",
                name: "支付结果",
                meta: { index: 1, title: '支付成功', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/payRes")
            },
            {
                path: "wallet",
                name: "我的钱包",
                meta: { index: 1, title: '我的钱包', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/wallet")
            },

            {
                path: "agreement",
                name: "协议",
                meta: { index: 1, title: '协议', showTab: false, requiresAuth: true },
                component: () => import("@/views/home/mine/agreement")
            },
        ]
    },
]

const router = new VueRouter({
    routes
})

// router.beforeEach(async (to, from, next) => {
//     let isLogin = store.state.user.isLogin
//     let data = store.state.app.defaultEquipment
//     // console.log("isLogin",isLogin);
//     if (to.path == '/realname') {
//         next()
//         return
//     }
//     if (isLogin !== '1') {
//         if (to.path != '/login' && to.path != '/error' && to.path != '/agreement') {
//             // console.log("未登录，拦截到登录页");
//             let code = $utils.func.getParamFromUrl(window.location.href.split("#")[0], "code")
//             if (code) {
//                 // 未登录但是有code
//                 let urls = window.location.origin
//                 window.location.href = urls + '/#/login'
//             } else {
//                 next('/login')
//             }
//         } else {
//             console.log("未登录，登录页或404或协议放行");
//             next()
//         }
//     } else if (!data.id && to.path != '/loading' && to.path != '/bind' && to.path !== '/error') {
//         console.log("已登录没有默认设备");
//         next('/loading')
//     } else {
//         console.log("已登录放行");
//         next()
//     }
// })

export default router
